'use strict'
const path = require('path');
const alias = require('./alias.config');
const projectConfig = require('./public/project.config');

function resolve(dir) {
    return path.join(__dirname, dir)
}

const port = 8020;
module.exports = {
    publicPath: './',
    outputDir: './dist',
    // assetsDir: 'static',
    lintOnSave: process.env.NODE_ENV === 'development',
    productionSourceMap: false,
    devServer: {
        host: "0.0.0.0",
        port: port,
        open: true,
        hotOnly: false,
        overlay: {
            warnings: false,
            errors: true
        },
        proxy: {
            // change xxx-api/login => mock/login
            // detail: https://cli.vuejs.org/config/#devserver-proxy
            [projectConfig.server_development]: {
                target: `http://localhost:${port}/mock`,
                changeOrigin: true,
                pathRewrite: {
                    ['^' + projectConfig.server_development]: ''
                }
            },
            // '/yiqing-api': {
            //     target: projectConfig.YIQING_HTTP_URL,
            //     changeOrigin: true,
            //     pathRewrite: {
            //         '^/yiqing-api': ''
            //     }
            // },
        },
        after: require('./mock/mock-server.js')
    },
    configureWebpack: alias,
    chainWebpack(config) {
        config.resolve.alias.set('src', path.join(__dirname, 'src'));

        config.plugins.delete('preload') // TODO: need test
        config.plugins.delete('prefetch') // TODO: need test

        // set svg-sprite-loader
        config.module
            .rule('svg')
            .exclude.add(resolve('src/icons'))
            .end()
        config.module
            .rule('icons')
            .test(/\.svg$/)
            .include.add(resolve('src/icons'))
            .end()
            .use('svg-sprite-loader')
            .loader('svg-sprite-loader')
            .options({
                symbolId: 'icon-[name]'
            })
            .end()

        // set preserveWhitespace
        config.module
            .rule('vue')
            .use('vue-loader')
            .loader('vue-loader')
            .tap(options => {
                options.compilerOptions.preserveWhitespace = true
                return options
            })
            .end()

        config
            // https://webpack.js.org/configuration/devtool/#development
            .when(process.env.NODE_ENV === 'development',
                config => config.devtool('cheap-source-map')
            )
    }
}
